package com.zm.demo.core.leetcode;

import javafx.util.Pair;

import java.util.Stack;

/**
 * Created by Administrator on 2020/1/11.
 */
public class MaxDepth {

    public int maxDepth(TreeNode root) {
        if (root == null){
            return 0;
        }
        Stack<Pair<TreeNode,Integer>> stack = new Stack<>();

        Integer maxDep = Integer.MIN_VALUE;
        stack.push(new Pair<>(root, 1));
        while (!stack.isEmpty()){

            Pair<TreeNode,Integer> p = stack.pop();
            maxDep = Math.max(maxDep, p.getValue());
            TreeNode _this = p.getKey();
            if (_this.left != null){
                stack.push(new Pair<>(_this.left, p.getValue()+1));
            }
            if (_this.right != null){
                stack.push(new Pair<>(_this.right, p.getValue()+1));
            }
        }

        return maxDep;
    }
}

